package EffectiveJava.chapter5;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Test {

    public static void main(String[] args) {
//        ArrayList<String> strings = new ArrayList<>();
//        unsafeAdd(strings, Integer.valueOf(42));
//        String s = strings.get(0);

//        HashSet hashSet = new HashSet();
//        hashSet.add(null);
//        hashSet.add("");

        Set<?> s1 = new HashSet<>();
        HashSet<String> strings = new HashSet<>(Arrays.asList("a", "b", "c"));
        HashSet<String> strings2 = new HashSet<>(Arrays.asList("a", "b", "c"));
        s1 = strings;
//        s1.add(strings2); //报错
//        s1.add(null);
//        s1.add("1");  //使用泛型<?>会出错,不加泛型则可是加入任何元素
    }

//    private static void unsafeAdd(List list, Object o){
//        list.add(o);
//    }
//    private static void unsafeAdd(List<Object> list, Object o){
//        list.add(o);
//    }

    private static int numElementsInCommon(Set<?> s1, Set<?> s2) {
        int result = 0;
        for(Object o1 : s1){
            if(s1.contains(o1)){
                result++;
            }
        }
        return result;
    }
}
